<script type="text/javascript">
(function(){
	<%
		local para = {
			ddns = {config = {}}
		}

		local data = tpl_get_data(para, true)
	-%>

	var ddnsInfo = <%=data%>;

	var providerList = [{name:label.oray, value:"oray.com"},
						{name:label.pubyun, value:"pubyun.com"}];

	function init(result){
		var errNo = result[ERR_CODE];
		var ret;
		if (E_NONE == errNo){
			ret = result[K_MODULE];
		}else{
			console.log(result);
			return;
		}

		var ddns = ret.ddns.config;
		initSwitch("Switch", ddns.enable, function(state){
			if (1 == state){
				$("#Param").show();
			}else{
				$("#Param").hide();
			}
		});

		initSelect("Provider", providerList, ddns.provider);

		$("#UserName").val(ddns.user);
		$("#Pwd").val(ddns.password);
		$("#HostName").val(ddns.domain);
	}

	init(ddnsInfo);

	function errHandle(errNo)
	{
		var noteStr = "";

		switch (errNo)
		{
		case E_NONE:
			return false;
		case E_DDNSHOSTBLANK:
			noteStr = errStr.ddnsHostNameEmpty;
			break;
		case E_INVDDNSHOST:
			noteStr = errStr.ddnsHostNameInv;
			break;
		case E_DDNSPWDBLANK:
			noteStr = errStr.ddnsPwdEmpty;
			break;
		case E_INVDDNSPWD:
			noteStr = errStr.ddnsPwdInv;
			break;
		case E_DDNSNAMEBLANK:
			noteStr = errStr.ddnsNameEmpty;
			break;
		case E_INVDDNSNAME:
			noteStr = errStr.ddnsNameInv;
			break;
		case E_DDNSSVRBLANK:
		case E_INVDDNSSVR:
		case E_DDNSMODE:
		default:
			noteStr = errStr.unknownErr + errNo;
			break;
		}

		showNote(noteStr);
		return true;
	}

	function checkUserName(username){
		if ("" == username){
			return E_DDNSNAMEBLANK;
		}

		if (E_NONE != checkStrLen(username, 64, 1)){
			return E_INVDDNSNAME;
		}

		return E_NONE;
	}

	function checkPassWord(password){
		if ("" == password){
			return E_DDNSPWDBLANK;
		}

		if (false == checkIllegalChar(password)){
			return E_INVDDNSPWD;
		}

		if (E_NONE != checkStrLen(password, 64, 1)){
			return E_INVDDNSPWD;
		}

		return E_NONE;
	}

	function checkDDNSDomain(domain){
		if ("" == domain){
			return E_DDNSHOSTBLANK;
		}

		if (false == checkDomain(domain)){
			return E_INVDDNSHOST;
		}

		return E_NONE;
	}

	$("#Save").click(function(){
		var data = {};
		var para = {};
		para.ddns = {};
		para.ddns.config = data;
		data.enable = $("#Switch").attr("data-value");
		data.provider = $("#Provider").attr("data-value");
		data.user = $("#UserName").val();
		data.password = $("#Pwd").val();
		data.domain = $("#HostName").val();

		if (errHandle(checkUserName(data.user))){
			return;
		}

		if (errHandle(checkPassWord(data.password))){
			return;
		}

		if (errHandle(checkDDNSDomain(data.domain))){
			return;
		}

		showLoading(label.saving);
		apiSet(para, function(ret){
			$.setTimeout(function(){
				closeLoading();
			}, 2000);

			errHandle(ret[ERR_CODE]);
		});
	});
})();
</script>
<div class="help hidden">
	<ul class="help-content">
		<li class="help-head">
			<img class="app-icon" src="/luci-static/images/app-icon/ddns.png?_=20170809165512" />
			<span class="help-title">{%label.ddns%}</span>
		</li>
		<li class="summary">{%helpStr.ddns%}</li>
		<li class="title">{%helpStr.ddnsProvider%}</li>
		<li class="content">{%helpStr.ddnsProviderOray%}</li>
		<li class="summary">{%helpStr.ddnsTip%}</li>
	</ul>
</div>
<div class="head-con">
	<img class="app-icon" src="/luci-static/images/app-icon/ddns.png?_=20170809165512" />
	<a href="javascript:history.go(-1);" class="head-btn-l">
		<i class="head-btn icon icon_back"></i>
		<span class="head-title">{%label.ddns%}</span>
	</a>
</div>
<div class="set-con below-head">
	<ul class="set-con set-con-switch">
		<p class="set-con-desc">
			<label class="tip-lbl">{%label.ddns%}</label>
			<span id="Switch" class="switch">
				<i class="switch-circle icon_select_off"></i>
			</span>
		</p>
	</ul>
	<div id="Param" class="hidden">
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.serviceProvider%}</label>
				<span id="Provider" class="select">
					<span class="value sel-value"></span>
					<i class="tip icon_list-expansion"></i>
				</span>
			</li>
		</ul>
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.ddnsName%}</label>
				<input id="UserName" class="text hover" maxLength="64" value="" />
			</li>
		</ul>
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.ddnsPwd%}</label>
				<input id="Pwd" class="text hover" type="password" maxLength="64" value="" /><i class="tip icon-eye" data-value="0"></i>
			</li>
		</ul>
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.hostName%}</label>
				<input id="HostName" class="text hover" maxLength="64" value="" />
			</li>
		</ul>
		<div class="err-note hidden">
			<i class="err-icon icon_remind_line"></i>
			<p class="note-str"></p>
		</div>		
	</div>
	<div class="btn-con">
		<input id="Save" class="btn" type="button" value="{%btn.save%}" />
	</div>
</div>